package com.css.test1.First;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.css.test1.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by css on 2017/11/7.
 */

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.vp_first)
    ViewPager vpFirst;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.ll_point)
    LinearLayout llPoint;
    @BindView(R.id.tab_)
    TabLayout tab;
    private List<ImageView> imageViews;
    private int last = 0;
    private List<String> title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_first);
        ButterKnife.bind(this);
        LayoutInflater inflater = getLayoutInflater();
        title = new ArrayList<>();
        title.add("第一页");
        title.add("第二页");
        title.add("第三页");
        imageViews = new ArrayList<>();
        imageViews.add((ImageView) inflater.inflate(R.layout.iv_welcome, null));
        imageViews.add((ImageView) inflater.inflate(R.layout.iv_welcome, null));
        imageViews.add((ImageView) inflater.inflate(R.layout.iv_welcome, null));
        tab.setupWithViewPager(vpFirst);
        vpFirst.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                llPoint.getChildAt(last).setBackground(getResources().getDrawable(R.drawable.point_white_shape));
                last = position;
                llPoint.getChildAt(position).setBackground(getResources().getDrawable(R.drawable.point_shape));

                if (position == imageViews.size() - 1) {


                    btEnter.setVisibility(View.VISIBLE);
                } else {
                    btEnter.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        PagerAdapter myAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return view == o;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(imageViews.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageViews.get(position));
                return imageViews.get(position);
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        };
        vpFirst.setAdapter(myAdapter);
    }


}
